-- NewClothesRender 对Res.NewClothesNode的解析 适用所有装备显示

local ClassRef = app.mvc.createRenderClass(...)


function ClassRef.create(node, isMultipleLayerNode)
	if not node then
		node = display.createMultipleLayerRenderWithCsbPath(Res.NewClothesNode, isMultipleLayerNode)
	end
	local sprite = ClassRef.extend(node)
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._isTouchTip = false

	self:setContentSize(self:getContentSize())

	self:setSelected(false)
	self:addTouchEventListener(function(sender, eventType)
		if eventType == ccui.TouchEventType.ended then
			-- 清理isNew
			if self._data then
				local clothes = UD:getClothesById(self._data)
				if clothes and clothes.isNew then
				    clothes:setIsNewGet(false)
				    self:setIsNewShown(false)

				    local idx = Constant.FASHION_LOCATION_2_CLOTHES_IDX[clothes:getLocation()]
				    UD.redPoint:checkStateByKey(RedPointConstant["Clothes_" .. idx])
				end
			end

			local oldSelected = self:isSelected()
			self:setSelected( true )
			if self._isTouchTip then
				self:setSelected(false)
			else
				if not oldSelected then
					executeFunc(self._onClick, self)
					if self._model then
						self._model:dispatchEvent("click", self)
					end
				end
			end
		end
	end)

	-- self:refresh()
end

-- data = fashionID
function ClassRef:refresh()
	local renderData = self._data
	if renderData then
		--dump(self._data, "ClothesRender data")
		self:refreshState()
		self._ui["grade"]:setVisible(true)
		self._ui["icon"]:setVisible(true)
		local conf = GD:getClothesConfig( renderData )
		local resConf = GD:getClothesResConfig( renderData )
		if conf and resConf then
			self._ui["grade"]:loadTexture( Res.Qulity_Icon_Bg[tonumber( conf.quality )] )
			self._ui["icon"]:loadTexture( GD:formatIconFilePath(Enums.ItemBagType.equip, resConf.icon) )
			self._ui["mask"]:setVisible( not UD:isClothesActive( renderData ) ) -- 是否激活了

			self:setIsNewShown( UD:isNewGetClothes( renderData ) )

			if conf.type2icon then
				self._ui["champion"]:setVisible(true)
				self._ui["champion"]:loadTexture(string.format("%s%s.png",Res.commonUIPath,conf.type2icon))
			else
				self._ui["champion"]:setVisible(false)
			end

		else
			self._ui["mask"]:setVisible(false)
			self:setIsNewShown(false)
			self._ui["grade"]:loadTexture( Res.Qulity_Icon_Bg[1] )
			self._ui["champion"]:setVisible(false)
		end
		self:setClickEnable( true )

	else
		self._ui["grade"]:setVisible(false)
		self._ui["icon"]:setVisible(false)

		-- self._ui["clothes_bg"]:setVisible( true )

		self:setClickEnable( false )

		self._ui["mask"]:setVisible(false)

		self:setSelected(false)
		self._ui["champion"]:setVisible(false)
	end
end

function ClassRef:refreshState()
	if self._model then
		self:setSelected(self._model:getUIState("selected") == true)
	end
end

-- 时装位DefPos处理
function ClassRef:setClothesDefPos(pos)
	self._clothesPos = pos
	self._ui["icon"]:loadTexture(GD:getEquipPosDefIcon(pos))
end

function ClassRef:getClothesDefPos()
	return self._clothesPos
end

function ClassRef:isSelected()
	return self._ui["selected"]:isVisible()
end
function ClassRef:setSelected(b)
	self._ui["selected"]:setVisible( b )
	if self._model then
		self._model:setUIState("selected", b) 
	end
end
function ClassRef:setClickEnable(b)
	self:setTouchEnabled(b)
end

-- 新获得标记是否显示
function ClassRef:setIsNewShown(b)
	self._ui["dot"]:setVisible(b)
end

function ClassRef:setMaskShown(b)
	self._ui["mask"]:setVisible(b)
end

function ClassRef:setClickHandler(func)
	self._onClick = func
end

-- listview 必须的方法
function ClassRef:setCellModel(model)
	self._model = model
end

return ClassRef
